package cn.gcyl.module.biz.service.privacy;

import cn.gcyl.framework.common.pojo.PageResult;
import cn.gcyl.framework.common.util.object.BeanUtils;
import cn.gcyl.module.biz.controller.admin.privacy.vo.*;
import cn.gcyl.module.biz.dal.dataobject.privacy.PrivacyDO;
import cn.gcyl.module.biz.dal.mysql.privacy.PrivacyMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static cn.gcyl.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.gcyl.module.system.enums.ErrorCodeConstants.PRIVACY_NOT_EXISTS;

/**
 * 隐私政策 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class PrivacyServiceImpl implements PrivacyService {

    @Resource
    private PrivacyMapper privacyMapper;

    @Override
    public Long createPrivacy(PrivacySaveReqVO createReqVO) {
        // 插入
        PrivacyDO privacy = BeanUtils.toBean(createReqVO, PrivacyDO.class);
        privacyMapper.insert(privacy);

        // 返回
        return privacy.getId();
    }

    @Override
    public void updatePrivacy(PrivacySaveReqVO updateReqVO) {
        // 校验存在
        validatePrivacyExists(updateReqVO.getId());
        // 更新
        PrivacyDO updateObj = BeanUtils.toBean(updateReqVO, PrivacyDO.class);
        privacyMapper.updateById(updateObj);
    }

    @Override
    public void deletePrivacy(Long id) {
        // 校验存在
        validatePrivacyExists(id);
        // 删除
        privacyMapper.deleteById(id);
    }

    @Override
        public void deletePrivacyListByIds(List<Long> ids) {
        // 删除
        privacyMapper.deleteByIds(ids);
        }


    private void validatePrivacyExists(Long id) {
        if (privacyMapper.selectById(id) == null) {
            throw exception(PRIVACY_NOT_EXISTS);
        }
    }

    @Override
    public PrivacyDO getPrivacy(Long id) {
        return privacyMapper.selectById(id);
    }

    @Override
    public PageResult<PrivacyDO> getPrivacyPage(PrivacyPageReqVO pageReqVO) {
        return privacyMapper.selectPage(pageReqVO);
    }

}